package com.jieshuibao.util.push;
import java.util.HashMap;
import java.util.Map.Entry;

import net.sf.json.JSONObject;


public class Push {
	private String appkey ="589a7ccf310c93551f000942";
	private String appMasterSecret ="dds9rlu0ef225gpuym7h3wwimffqm4iv";
	private String appkeyIos ="5779d5e3e0f55a6e590030ad";
	private String appMasterSecretIos ="jycimsztxfhyrl3kvpsieugtz2oh51ve";
	private String timestamp = null;
	
	private PushClient client = new PushClient();
	
	
	
/*	public Demo(String key, String secret) {
		try {
			appkey = key;
			appMasterSecret = secret;
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}*/
	
	public void testPush(){
		JSONObject notification=new JSONObject();
		long currentTimeMillis = System.currentTimeMillis();
		timestamp=""+currentTimeMillis+"";
		notification.put("appkey", "589a7ccf310c93551f000942");
		notification.put("timestamp", timestamp);
		notification.put("type", "customizedcast");//单播  customizedcast  指定设备
		//notification.put("device_tokens", "AktFVd6CUKqT5vMj8ur_od2N-A0WnJfVz7oOH9IMlrMU");
		notification.put("alias_type", "alias");
		notification.put("alias", "ceshiji");//设备别名
		
		JSONObject payload=new JSONObject();
		
		
		payload.put("display_type", "notification");
		
		payload.put("display_type", "notification");
		
		JSONObject body=new JSONObject();
		
		
		body.put("ticker", "通知栏提示文字");
		body.put("title", "通知栏标题");
		body.put("text", "通知内容");
		body.put("after_open", "go_app");
		body.put("after_open", "go_activity");
		body.put("activity", "/question/pageList");
		
		JSONObject extra=new JSONObject();
		extra.put("sinceId", 1);
		extra.put("maxId", 10);
		
		payload.put("body", body);
		//payload.put("extra", extra);
		notification.put("payload", payload);
		/* "go_app": 打开应用
         "go_url": 跳转到URL
         "go_activity": 打开特定的activity
         "go_custom": 用户自定义内容。*/
		
		try {
			client.sendThree(notification.toString(),appMasterSecret);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
	public void testPushIos(){
		JSONObject notification=new JSONObject();
		long currentTimeMillis = System.currentTimeMillis();
		timestamp=""+currentTimeMillis+"";
		notification.put("appkey", "5779d5e3e0f55a6e590030ad");
		notification.put("timestamp", timestamp);
		notification.put("type", "customizedcast");//单播  customizedcast  指定设备
		//notification.put("device_tokens", "025a936aa1f93b3eda024b45611d25d4e67c8e12a1f10f498fa6cc413d0fa615");
		notification.put("alias_type", "alias");
		notification.put("alias", "alias1310");//设备别名
		
		JSONObject payload=new JSONObject();
		JSONObject aps=new JSONObject();
		aps.put("alert", "通知栏提示文字");
		aps.put("badge", "通知栏标题");
		aps.put("sound", "通知内容");
		payload.put("sinceId", 1);
		payload.put("maxId", 10);
		
		payload.put("aps", aps);
		//payload.put("extra", extra);
		notification.put("payload", payload);
		/* "go_app": 打开应用
         "go_url": 跳转到URL
         "go_activity": 打开特定的activity
         "go_custom": 用户自定义内容。*/
		
		try {
			client.sendThree(notification.toString(),appMasterSecretIos);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}

	public void sendAndroidCustomizedcast(PushType pushType) throws Exception {
		AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appkey,appMasterSecret);
		
		customizedcast.setTicker(pushType.getTicker());
		customizedcast.setTitle(pushType.getTitle());
		customizedcast.setTicker(pushType.getTicker());
		customizedcast.setTitle(pushType.getTitle());
		customizedcast.setText(pushType.getText());
		customizedcast.setAlias(pushType.getAlias(), "alias");
		//
		String open = pushType.getOpen();
		
		if(open.equals("0")){
			customizedcast.goAppAfterOpen();
		}else if(open.equals("1")){
			JSONObject json = JSONObject.fromObject(pushType.getMap()); 
			customizedcast.goCustomAfterOpen(json);
		}
		HashMap<String,String> map = pushType.getMap();
		if(map!=null){
			customizedcast.setExtraField("key", "test");
			for (Entry<String, String> entry : map.entrySet()) {  
				customizedcast.setExtraField(entry.getKey(), entry.getValue());
			}  
		}
		
		customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		customizedcast.setProductionMode();
		client.sendTwo(customizedcast);
	}
	public void sendAndroidUnicast() throws Exception {
		AndroidUnicast unicast = new AndroidUnicast(appkey,appMasterSecret);
		
		unicast.setDeviceToken("AktFVd6CUKqT5vMj8ur_od2N-A0WnJfVz7oOH9IMlrMU");
		
		unicast.setTicker("通知栏提示文字");
		unicast.setText("通知内容");
		
		unicast.setTitle("中文的通知栏标题");
		unicast.goAppAfterOpen();
		unicast.setTicker("通知栏提示文字");
		
		unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);

		unicast.setProductionMode();
		//unicast.setTestMode();
		// Set customized fields
		//unicast.setExtraField("test", "helloworld");
		client.sendTwo(unicast);
	}
	
	public void sendIOSCustomizedcast(PushType pushType) throws Exception {
		IOSCustomizedcast customizedcast = new IOSCustomizedcast(appkeyIos,appMasterSecretIos);
		customizedcast.setBadge(1);
		customizedcast.setSound("default");
		
		customizedcast.setAlert(pushType.getText());
		customizedcast.setBadge(1);
		customizedcast.setSound("default");
		customizedcast.setAlias(pushType.getAlias(), "alias");
		HashMap<String,String> map = pushType.getMap();
		for (Entry<String, String> entry : map.entrySet()) {  
			customizedcast.setCustomizedField(entry.getKey(), entry.getValue());
		}  
		
		customizedcast.setTestMode();
		client.sendTwo(customizedcast);
	}
	public void sendIOSUnicast() throws Exception {
		IOSUnicast unicast = new IOSUnicast(appkeyIos,appMasterSecretIos);
		// TODO Set your device token
		unicast.setDeviceToken( "0ef163f3187e555251963b8a711b0e5785ab54dc4b6802bf990b785201a0cfde");
		unicast.setBadge(0);
		unicast.setSound("default");
		unicast.setAlert("IOS 单播测试");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		unicast.setCustomizedField("test", "helloworld");
		unicast.setCustomizedField("test", "helloworld");
		unicast.setCustomizedField("test", "helloworld");
		//unicast.setProductionMode();
		unicast.setTestMode();
		// Set customized fields
		client.sendTwo(unicast);
		
	}
	public static void main(String[] args) {
		Push demo = new Push();
		PushType pushType = new PushType();
		
		try {
			//demo.testPush();
			HashMap<String, String> map=new HashMap<String, String>();
			//android
		/*	pushType.setTitle(PushType.TITLE_0);
			pushType.setAlias("ceshiji");
			pushType.setOpen("1");
			pushType.setTicker(PushType.TICKER);
			pushType.setText(PushType.TEXT_0);
			map.put("type", "1");
			map.put("name", "我的悬赏");
			pushType.setMap(map);*/
			
			
			//ios
			pushType.setText(PushType.TEXT_0_1);
			pushType.setAlias("alias1310");
			map.put("type", "2");
			map.put("name", "答案被采纳");
			map.put("type", "2");
			map.put("name", "答案被采纳");
			pushType.setMap(map);
			demo.sendIOSCustomizedcast(pushType);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
